DOUBLE PRECISION function soal_2_rekayasa(x)
	IMPLICIT NONE
	double precision cf,pf,x
	common /soal_2_rekayasa_global/ cf,pf
	
	soal_2_rekayasa = (cf - 1) / pf
END

DOUBLE PRECISION function soal_4_rekayasa(x)
	IMPLICIT NONE
	double precision cf,pf,x, pi, theta
	common /soal_4_rekayasa_global/ pi,theta
	
	soal_4_rekayasa = 1/(1-(sin(theta/2))**2*(sin(x))**0.5)
END


subroutine hitung_soal_2_rekayasa(metode,p,n)
! metode (1-4)
! p tekanan (1-10):{1,10,20,40,60,80,120,160,250,400}
	implicit none
	integer*2 metode, p, n
	external soal_2_rekayasa
	external ATURAN_TRAPESIUM, simpson_1_per_3, ATURAN_GL, ROMBERG
	double precision a, b, hasil, delta, x, sigma, temp,Cp(10),Pp(10), cf, pf
	double precision f1, f2
	common /soal_2_rekayasa_global/ cf, pf 
	Pp(1) = 1
	Pp(2) = 10
	Pp(3) = 20
	Pp(4) = 40
	Pp(5) = 60
	Pp(6) = 80
	Pp(7) = 120
	Pp(8) = 160
	Pp(9) = 250
	Pp(10) = 400
	
	Cp(1) = 0.9940
	Cp(2) = 0.9370
	Cp(3) = 0.8683
	Cp(4) = 0.7043
	Cp(5) = 0.4515
	Cp(6) = 0.3429
	Cp(7) = 0.4259
	Cp(8) = 0.5252
	Cp(9) = 0.7468
	Cp(10) = 1.9080
	
	cf = Cp(p)
	pf = Pp(p)
	!a,b untuk arah x dan y
	a = 0
	b = pf
	x = a
	
	select case (metode)
		case (1)
			print*,"Metode Aturan Trapesium"
			call ATURAN_TRAPESIUM(a,b,n,temp,soal_2_rekayasa)
			hasil = pf*exp(temp)
			print*,"Solusi ",hasil
		case (2)
			if (mod(n,2).eq.0) then
				print*,"Metode Aturan 1/3 Simpson"
				call simpson_1_per_3(a,b,n,temp,soal_2_rekayasa)
				hasil = pf*exp(temp)
				print*,"Solusi ",hasil
			else
				print*,"Nilai n = ",n," tidak bernilai genap"
			end if
		case (3)
			print*,"Metode Romberg"
			call ROMBERG(a,b,n,temp,soal_2_rekayasa)
			hasil = pf*exp(temp)
			print*,"Solusi ",hasil
		case (4)
			if (n.EQ.3) then
				print*,"Metode Gauss-Legendre orde 3"
			else if (n.EQ.4) then
				print*,"Metode Gauss-Legendre orde 4"
			else
				n = 5
				print*,"Metode Gauss-Legendre orde 5"
			end if
			call ATURAN_GL(a,b,temp,n,soal_2_rekayasa)
			hasil = pf*exp(temp)
			print*,"Solusi ",hasil
		
	end select	
end

subroutine hitung_soal_4_rekayasa()
	implicit none
	integer*2 i,n
	external soal_4_rekayasa
	external ATURAN_GL
	double precision pi, theta, a, b, temp
	common /soal_4_rekayasa_global/ pi, theta 
	
	pi = 4*atan(1.0)
	a = 0
	b = pi/2
	
	print*,"Metode Gauss-Legendre orde 4"	
	do i = 1,10
		theta = i*pi/20
		call aturan_trapesium(a,b,temp,n,soal_4_rekayasa)
		print*,"Theta = ",i,"pi/20, hasil = ", temp
	end do
end